package com.fcscs.cservice.client.main.inroomdining;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.fcscs.core.common.MainPaginationSearchModel;
import com.fcscs.core.common.authority.UserAuthority;
import com.fcscs.core.domain.IdNameModel;
import com.fcscs.core.domain.Property;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.CoreEnums.SubmitType;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.SearchExecutor;
import com.fcscs.core.gwt.common.SessionContext;
import com.fcscs.core.gwt.common.ui.FcsFields.ClearElementCombobox;
import com.fcscs.core.gwt.common.ui.FcsFields.EasyClearCombobox;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.core.gwt.common.ui.FcsPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.CserviceControllerUtils;
import com.fcscs.cservice.client.common.TextFieldAndIconButtonField;
import com.fcscs.cservice.client.common.TextFieldAndIconButtonField.TextFieldListener;
import com.fcscs.cservice.client.common.model.configuration.reservation.PropertyIdNameModelData;
import com.fcscs.cservice.client.common.model.inroomdining.InRoomDiningModel;
import com.fcscs.cservice.client.common.model.inroomdining.InRoomDiningModelData;
import com.fcscs.cservice.client.common.model.restaurants.RestaurantsModelData;
import com.fcscs.cservice.domain.Restaurant;
import com.fcscs.cservice.domain.model.BookingView;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Widget;

public class InRoomDiningPanel extends FcsPanel {
	private VerticalPanel vp;
	private FormPanel myFormPanel;
	private EasyClearCombobox<PropertyIdNameModelData> propCombox;
	private ListStore<PropertyIdNameModelData> propList = new ListStore<PropertyIdNameModelData>();
	private ClearElementCombobox<RestaurantsModelData>restaCombox;
	private ListStore<RestaurantsModelData> restaList = new ListStore<RestaurantsModelData>();
	private HorizontalPanel hp1;
	private HorizontalPanel hp2;
	private TextFieldAndIconButtonField searchFieldBtn;
	private FormButton advancedSearchButton;
	private InRoomDiningGrid grid;
	private InRoomDiningAdvancedDialog inAdvancedDialog;

	public InRoomDiningPanel(String id) {
		super(id);
	}

	@Override
	public void init() {
		vp = new VerticalPanel();
		vp.setSpacing(5);

		myFormPanel = new FormPanel();
		myFormPanel.setHeading(CoreConstants.CORE_MESSAGES.operation(
				CServiceConstants.CSERVICE_CONSTANTS.in_RoomDining(), CoreConstants.CORE_CONSTANTS.search()));
		myFormPanel.setSize(960, 100);
		myFormPanel.setFrame(true);
		myFormPanel.setId("InRoomDiningPanel_myFormPanel");
		vp.add(myFormPanel);

		hp1 = new HorizontalPanel();
		hp1.setSpacing(5);
		hp1.setId("InRoomDiningPanel_hp1");

		hp2 = new HorizontalPanel();
		hp2.setSpacing(5);
		hp2.setId("InRoomDiningPanel_hp2");

		LayoutContainer container1 = new LayoutContainer();
		container1.setLayout(new ColumnLayout());

		LayoutContainer container2 = new LayoutContainer();
		container2.setLayout(new ColumnLayout());

		Label propLabel = new Label(CoreConstants.CORE_CONSTANTS.property()
				+ CoreConstants.REQURIED_FIELD_LABEL);
		propLabel.setId("InRoomDiningPanel_propLabel");
		propLabel.setWidth(150);

		Label restaLabel = new Label(CServiceConstants.CSERVICE_CONSTANTS.restaurant()
				+ CServiceConstants.FIELD_SEP);
		restaLabel.setId("InRoomDiningPanel_restaLabel");
		restaLabel.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "80px");
		restaLabel.setWidth(150);

		propCombox = new EasyClearCombobox<PropertyIdNameModelData>();
		propCombox.setId("DevicePanel_propCombox");
		propCombox.setWidth(150);
		propCombox.setDisplayField(CoreConstants.FULLTNAME);
		propCombox.setForceSelection(true);
		propCombox.setTriggerAction(TriggerAction.ALL);
		propCombox.setStore(propList);
		propCombox.setAllowBlank(false);
		propCombox.addSelectionChangedListener(new SelectionChangedListener<PropertyIdNameModelData>() {
			
			@Override
			public void selectionChanged(SelectionChangedEvent<PropertyIdNameModelData> se) {
				if (null != propCombox.getValue()) {
					Long propId = propCombox.getValue().getEntity().getId();
					CserviceControllerUtils.getInstance().getRestaurantList(propId, restaList);
//					newSearch(true);
				}
			}
		});

		restaCombox = new ClearElementCombobox<RestaurantsModelData>(new RestaurantsModelData(new Restaurant()));
		restaCombox.setId("DevicePanel_propCombox");
		restaCombox.setWidth(150);
		restaCombox.setDisplayField(CoreConstants.FULLTNAME);
		restaCombox.setForceSelection(true);
		restaCombox.setTriggerAction(TriggerAction.ALL);
		restaCombox.setStore(restaList);

		container1.add(propLabel, new ColumnData(80));
		container1.add(propCombox, new ColumnData(150));
		container1.add(restaLabel, new ColumnData(80));
		container1.add(restaCombox, new ColumnData(150));
		hp1.add(container1);
		myFormPanel.add(hp1);

		Label l = new Label(CoreConstants.CORE_CONSTANTS.search() + CServiceConstants.FIELD_SEP);
		container2.add(l, new ColumnData(80));

		searchFieldBtn = new TextFieldAndIconButtonField(new TextFieldListener() {

			@Override
			public void onTextFieldEnterKeyPressSearch(String value) {
				// if (value == null || value.trim().isEmpty()) {
				// newSearch(true);
				// } else {
				// newSearch(false);
				// }
			}

			@Override
			public void onIconButtoSearch(String value) {
				// if (value == null || value.trim().isEmpty()) {
				// newSearch(true);
				// } else {
				// newSearch(false);
				// }
			}
		});
		container2.add(searchFieldBtn, new ColumnData(230));

		advancedSearchButton = new FormButton(CServiceConstants.CSERVICE_CONSTANTS.advancedSearch());
		advancedSearchButton.setId("ReservationPanel_advancedSearchButton");
		advancedSearchButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				inAdvancedDialog = InRoomDiningAdvancedDialog
						.getInstance(new FormSubmitListener<BookingView>() {

							@Override
							public void formSubmitCallBack(FcsFormPanel<BookingView> form,
									SubmitType submitType) {
//								newSearch(true);
//								restaurantsDetailDialog.hide();
							}
						});
				inAdvancedDialog.show();
			}
		});
		container2.add(advancedSearchButton, new ColumnData(120));

		hp2.add(container2);
		myFormPanel.add(hp2);

		grid = new InRoomDiningGrid(CServiceConstants.CSERVICE_CONSTANTS.in_RoomDining(),
				new SearchExecutor<InRoomDiningModelData, MainPaginationSearchModel>() {

					@Override
					public void doSearch(MainPaginationSearchModel searchModel,
							AsyncCallback<BasePagingLoadResult<InRoomDiningModelData>> callback) {
						List<InRoomDiningModelData> inRoomList = new ArrayList<InRoomDiningModelData>();
						InRoomDiningModel model1 = new InRoomDiningModel();
						model1.setRoomNo("bi001");
						model1.setGuestName("guest001");
						model1.setCategroy("cate001");
						model1.setDeliveryDate("2012-4-26");
						model1.setDeliveryTime("15:07");
						model1.setStatus("pending");
						inRoomList.add(new InRoomDiningModelData(model1));

						InRoomDiningModel model2 = new InRoomDiningModel();
						model2.setRoomNo("bi002");
						model2.setGuestName("guest002");
						model2.setCategroy("cate002");
						model2.setDeliveryDate("2012-4-27");
						model2.setDeliveryTime("18:03");
						model2.setStatus("completed");
						inRoomList.add(new InRoomDiningModelData(model2));
						callback.onSuccess(FcsPanel.createPagingLoadResult(
								inRoomList, searchModel.getCurrentPage(),
								searchModel.getResultsPerPage(), inRoomList.size()));
					}
				});

		vp.add(grid);
		add(vp);
		onShow();
	}

	@Override
	public UserAuthority getRequiredAuthority() {
		return null;
	}

	@Override
	protected Widget getViewPanel() {
		return null;
	}

	@Override
	public void onShow() {
		loadPropStore();
		newSearch(true);
	}
	
	protected void newSearch(boolean all) {
		String term = null;
		if (all) {
			term = null;
		}else {
			term = searchFieldBtn.getTextFieldValue().trim();
		}
		MainPaginationSearchModel sm = new MainPaginationSearchModel();
		sm.setSearchTerm(term);
		grid.newSearch(sm);
	}
	
	
	public void loadPropStore() {
		propList.removeAll();
		List<IdNameModel<Property>> listIdNameModel = SessionContext.getInstance().getPropertyGroup();
		IdNameModel<Property> noGroup = new IdNameModel<Property>(SessionContext.getInstance()
				.getPropertyId(), SessionContext.getInstance().getPropertyName());
		if (listIdNameModel.size() == 0) {
			propList.add(new PropertyIdNameModelData(noGroup));
		} else {
			for (IdNameModel<Property> inm : listIdNameModel) {
				propList.add(new PropertyIdNameModelData(inm));
			}
		}
		propCombox.setValue(propList.findModel(CoreConstants.ID, SessionContext.getInstance()
				.getPropertyId()));
	}
	
	@Override
	public boolean onHide() {
		return true;
	}

}
